import { Article, History } from '@/types/data'
import { SearchAction } from '@/types/store'
import { getLocalHistories } from '@/utils/auth'
type SearchSuggest = {
  searchOption: string[]
  searchHistory: History
  searchResults: { search_total: number; res: Article[] }
}
// 用户信息的reducer
function searchReducer (
  state: SearchSuggest = {
    searchOption: [],
    searchHistory: getLocalHistories(),
    searchResults: { search_total: -1, res: [] }
  },
  action: SearchAction
) {
  switch (action.type) {
    // 存储 用户信息
    case 'search/suggest':
      return { ...state, searchOption: action.payload }
    case 'search/history':
      return { ...state, searchHistory: action.payload }
    case 'search/results':
      return {
        ...state,
        searchResults: {
          search_total: action.payload.search_total,
          res: [...state.searchResults.res, ...action.payload.res]
        }
      }
    case 'search/clear':
      return { ...state, searchResults: { search_total: -1, res: [] } }
    default:
      return state
  }
}

export default searchReducer
